package com.freesun.shop.listener;

import com.freesun.shop.model.WechatMessage;
import com.freesun.shop.service.SmsPushService;
import com.freesun.shop.service.WechatPushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.*;

@Component
public class SmsValidationCodeListener implements MessageListener {
    private static Logger logger = LoggerFactory.getLogger(SmsValidationCodeListener.class);

    @Autowired
    private SmsPushService smsPushService;
    @Override
    @JmsListener(destination = "validation.code.sms.queue",concurrency = "1")
    public void onMessage(Message message) {
        logger.info("收到消息了:{}", message);
        try {
            //短信为文本消息
            TextMessage textMessage= (TextMessage) message;
            String text = textMessage.getText();
            smsPushService.pushSms(text);
            message.acknowledge(); //签收消息
            logger.info("短信消息消费成功！");
        } catch (JMSException e) {
            logger.info("短信消息消费失败！");
            e.printStackTrace();
        }

    }
}
